package com.ig.business;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.a2s.model.Item;
import com.amazonaws.a2s.model.ItemAttributes;
import com.amazonaws.a2s.model.Items;
import com.ig.amazon.service.AmazonService;
import com.ig.amazon.service.AmazonServiceFactory;
import com.ig.dao.BookDAO;
import com.ig.dao.DAOFactory;
import com.ig.model.Book;
import com.ig.model.Search;

/**
 * BookInfoManager directs the calls to DB & Amazon services.
 * 
 * @author skodali
 * 
 */
public class BookInfoManager {

	private static AmazonService amazonService;

	private static BookDAO bookDAO;

	static {
		amazonService = AmazonServiceFactory.getAmazonService();
		bookDAO = DAOFactory.getBookDAO();
	}

	/** Stores Book info into Local DB by getting the info from Amazon */
	private static Book getBookInfoFromAmazonAndStore(String ean) {
		System.out.println("getBookInfoFromAmazonAndStore");
		Book book = getBookInfoFromAmazon(ean);
		// store book info into Local DB
		if (book != null && getBookInfoFromLocalService(book.getEan()) == null)
			bookDAO.storeBookInfo(book);
		return book;
	}

	/** gets Book Details from Local DB, if it is available in Local DB */
	private static Book getBookInfoFromLocalService(String ean) {
		return bookDAO.getBookInfo(ean);
	}

	/** gets Book Info from Amazon */
	private static Book getBookInfoFromAmazon(String ean) {
		Book book = null;
		try {
			Item item = amazonService
					.retrieveProductFromAmazonECS("Books", ean);
			if (item != null)
				book = populateBookDetails(item);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return book;
	}

	/** gets Book Info from Amazon */
	public static List<Book> getBooksFromAmazon(Search searchCriteria) {
		List<Book> books = null;
		try {
			List<Items> items = amazonService
					.retrieveProductsFromAmazon(searchCriteria);
			if (items != null && items.size() > 0)
				books = populateBooks(items);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return books;
	}

	/** populates book Details from Item Object details */
	private static Book populateBookDetails(Item item) {
		Book book = new Book();
		if (item.isSetItemAttributes()) {
			ItemAttributes itemAttributes = item.getItemAttributes();
			book.setAuthor(itemAttributes.getAuthor());
			book.setTitle(itemAttributes.getTitle());
			book
					.setListPrice(itemAttributes.getListPrice()
							.getFormattedPrice());
			book.setEan(itemAttributes.getEAN());
		}
		return book;
	}

	/** populates book-list from listItems */
	private static List<Book> populateBooks(List<Items> listItems) {
		ItemAttributes itemAttributes = null;
		Book book = null;
		List<Book> books = new ArrayList<Book>();
		for (Items items : listItems)
			for (Item item : items.getItem()) {
				if (item.isSetItemAttributes()) {
					itemAttributes = item.getItemAttributes();
					book = new Book();
					book.setAuthor(itemAttributes.getAuthor());
					book.setTitle(itemAttributes.getTitle());
					book.setListPrice(itemAttributes.getListPrice()
							.getFormattedPrice());
					book.setEan(itemAttributes.getEAN());
				}
				if (item.isSetOfferSummary()) {
					book.setLowestNewPrice(item.getOfferSummary()
							.getLowestNewPrice().getFormattedPrice());
					book.setLowestUsedPrice(item.getOfferSummary()
							.getLowestUsedPrice().getFormattedPrice());
				}
				books.add(book);
			}
		return books;
	}

	/**
	 * gets Book Information from Amazon or Local DB.
	 * 
	 * @param isbn
	 * @return
	 */
	public static Book getBookInfo(String isbn) {
		// CHECK book availability in local DB
		Book book = getBookInfoFromLocalService(isbn);
		book = (book != null) ? book : getBookInfoFromAmazonAndStore(isbn);
		return book;
	}

}
